from flask import Blueprint, request, jsonify
import os
import time
from flask_demo.entities import ImgComponent
from flask_demo import app_database

from flask_demo.common.MyJsonEncoder import MyJsonEncoder

import json

img_component_blue_print = Blueprint("img_component_blue_print", __name__, url_prefix="/img_component")


@img_component_blue_print.route("/mine", methods=["get", ])
def get_img_component_mine():
    user_id = request.args.get("user_id")
    success = False
    img_component_list = []
    if user_id is not None:
        img_component_list = ImgComponent.query.filter(ImgComponent.author_id == int(user_id)).all()
        print("get_img_component_mine: ", user_id, img_component_list)
        success = True
        pass
    return {
        "success": success,
        "data": [i.to_dict() for i in img_component_list]
    }


@img_component_blue_print.route("/all", methods=["get", ])
def get_img_component_all():
    success = False
    try:
        img_component_list = ImgComponent.query.all()
        success = True
    except:
        success = False
        pass
    return {
        "success": success,
        "data": {
            "dataList": [i.to_dict() for i in img_component_list]
        }
    }


@img_component_blue_print.route("/public", methods=["get", ])
def get_img_component_public():
    return {
        "ok": True,
        "dataList": []
    }


@img_component_blue_print.route("/new", methods=["post", ])
def post_img_component_new():
    img_component_id = request.json.get("img_component_id")
    author_id = request.json.get("author_id")
    title = request.json.get("title")
    img_url = request.json.get("img_url")
    group_name = request.json.get("group_name")
    text_str = request.json.get("text_str")
    new_img_component = ImgComponent(author_id, title, img_url, group_name, 0, text_str)
    print("post_img_component_new: ", author_id, title, img_url, group_name, text_str)
    if img_component_id < 0:
        app_database.session.add(new_img_component)
        app_database.session.commit()
        img_component_id = new_img_component.img_component_id
        pass
    else:
        pass
    return {
        "success": True,
        "data": {
            "img_component_id": img_component_id
        }
    }


@img_component_blue_print.route("/delete", methods=["post", ])
def post_img_component_delete():
    return {
        "ok": True,
    }


@img_component_blue_print.route("/publish", methods=["post", ])
def post_img_component_publish():
    return {
        "ok": True,
    }


@img_component_blue_print.route("/cancel", methods=["post", ])
def post_img_component_cancel():
    return {
        "ok": True,
    }


@img_component_blue_print.route("/file", methods=["post", ])
def post_img_component_file():
    basedir = os.path.abspath(os.path.dirname(__file__))  # 获取当前项目的绝对路径
    file_dir = os.path.join(basedir, "upload_files")  # 拼接成合法文件夹地址
    if not os.path.exists(file_dir):
        os.makedirs(file_dir)  # 文件夹不存在就创建
    f = request.files['img_component']  # 从表单的file字段获取文件，myfile为该表单的name值
    if f is not None:
        f_name = f.filename
        ext = f_name.rsplit('.', 1)[1]  # 获取文件后缀
        unix_time = int(time.time())
        new_filename = str(unix_time) + '.' + ext  # 修改文件名
        f.save(os.path.join(file_dir, new_filename))  # 保存文件到 upload 目录
        pass
    return {
        "ok": True,
        "url": "abc"
    }